﻿#include "OperNCTUserDefined.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "GUIDialog/GUIPhyscisDialog/NCTDialogUserDefined.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTUserDefined.h"

#include "updateBrowserTable.h"
#include "GUIWidget/AttributeSetDialog.h"

namespace ModelOper
{
    bool OperNCTUserDefined::execGUI()
    {
        //按钮点击事件触发
        if (_emitter == nullptr) return false;
        const QString name = _emitter->objectName();
        if (name == "actionUserDefineTreatment")
            createNCT();
        return false;
    }
    bool OperNCTUserDefined::execProfession()
    {
        return false;
    }
    void OperNCTUserDefined::createNCT()
    {
        //弹出对话框
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        GUI::NCTDialogUserDefined dlg(GUI::PhysicsDlgExecType::Create, -1, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }
    void OperNCTUserDefined::duplicateNCT(const int nctID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        GUI::NCTDialogUserDefined dlg(GUI::PhysicsDlgExecType::Duplicate, nctID, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }
    void OperNCTUserDefined::editNCT(const int nctID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        GUI::NCTDialogUserDefined dlg(GUI::PhysicsDlgExecType::Edit, nctID, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }
    void OperNCTUserDefined::removeNCT(const int nctID)
    {
        //获取管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsNCTManager* nctMgr = phys->getFITKAcousticsNCTManager();
        if (nctMgr == nullptr) return;
        //移除并更新列表
        nctMgr->removeDataByID(nctID);
        ModelOper::updateBrowserTable();
    }

    void OperNCTUserDefined::setAttribute(const QString & name, const QString & text, const int attrIndex, const QList<int>& nctID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsSpectraManager* specMgr = phys->getFITKAcousticsSpectraManager();
        if (specMgr == nullptr) return;
        Acoustics::FITKAcousticsNCTManager* nctMgr = phys->getFITKAcousticsNCTManager();
        if (nctMgr == nullptr) return;
        //设置对话框
        GUI::AttributeSetDialog dlg(2, mw);
        dlg.setTextLabel(name);
        QList<QString> list;
        QList<Acoustics::FITKAcousticsAbstractSpectra*> specList{};
        if (attrIndex == 1) 
            specList = specMgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Absorption);
        else if (attrIndex == 2 || attrIndex == 3)
            specList = specMgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::InsertionLoss);
        for (auto value : specList) {
            list += value->getDataObjectName();
        }
        dlg.setDefaultValueQString(list);
        if (dlg.exec() != QDialog::Accepted) return;

        QString spec = dlg.getValueQString();//需要getText
        int specID = specMgr->getDataByName(spec)->getDataObjectID();
        for (int id : nctID)
        {
            Acoustics::FITKAcousticsNCTUserDefined* nct = dynamic_cast
                <Acoustics::FITKAcousticsNCTUserDefined*>(nctMgr->getDataByID(id));
            if (nct == nullptr) continue;
            //不同列设置不同参数
            switch (attrIndex)
            {
            case 1: //吸声系数普
                nct->setAbsorptionCoeffID(specID); break;
            case 2: //插入损失
                nct->setInsertionLossResonantID(specID); break;
            case 3: //插入损失
                nct->setInsertionLossNonResonantID(specID); break;
            default: break;
            }
        }
        ModelOper::updateBrowserTable();
    }

}